<?php

namespace App\Http\Controllers\Admin;
use App\Model\Area;
use App\Model\Checkcategory;
use App\Model\Checkentry;
use App\Model\Checknegative;
use App\Model\Comcheck;
use App\Model\Comcheckagain;
use App\Model\Comcheckagaindetail;
use App\Model\Comcheckdetail;
use App\Model\Company;
use App\Model\Xtprovince;
use App\Model\Xtcity;
use App\Model\Xttown;
use App\Model\Xtstreet;
use App\Model\MessageText;
use App\Model\Message;
use App\Model\Job;
use App\Model\Department;
use App\Model\Users as User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Support\Facades\DB;
class DepartmentController extends Controller
{

    public function adduser(Request $request){
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'username' => 'required|min:2',
                'sex' => 'required',
                'mwork' => 'required',
                'moffwork' => 'required',
                'awork' => 'required',
                'aoffwork' => 'required',
                // 'cardnum'  => 'required|max:18|min:18'
            ],[
                'username.required' => '名称必填',
                // 'cardnum.required' => '身份证号必填',
                'username.min' => '名称不符合规范',
                // 'cardnum.max' => '身份证号格式不正确',
                // 'cardnum.min' => '身份证号格式不正确'
            ]);
            $user=new User();
            $user->name=$request['username'];

            $lastuser=User::where('type','1')->orderBy('id','desc')->first();
            $lastid=intval($lastuser->account)+1;
            $user->account=sprintf("%05d",$lastid);
            $user->department_id=$request['department_id'];
            $user->job_id=$request['job_id'];
            $user->duty=$request['duty'];
            $user->mwork=$request['mwork'];
            $user->moffwork=$request['moffwork'];
            $user->awork=$request['awork'];
            $user->aoffwork=$request['aoffwork'];

            $user->sex=$request['sex'];
            $user->password=bcrypt('123456');
            $user->type = 1;
            $user->role='normal';
            $user->save();
            return redirect()->to(route('admin.user.userlist'))->with(['status'=>'添加成功']);
        }
        $deps=Department::select('id','department')->get();
        $jobs=Job::select('id','job')->get();
        return view('admin/user/adduser',compact('deps','jobs'));
    }

    // 修改公司信息
    public function comedit(Request $req){
        $com=Company::with('commanager','safeOfficer')->where('id',$req['id'])->first();
        $provinces=Area::where('level',1)->select('id','name')->get();
        return view('admin/com/comedit',compact('com','provinces'));
    }
    //  获取省市区街道 社区 数组
    public function getareajson(Request $req){
        $level=$req['lvl'];
        if($level==1){
            $provinces=Area::where('level',1)->select('id','name')->get()->toArray();
            $data=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>$provinces
            ];
            return response()->json($data);
        }else{
            $code=$req['code'];
            $json=Area::where('level',$level)->where('pid',$code)->select('id','name')->get()->toArray();
            $data=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>$json
            ];
            return response()->json($data);
        }
    }
	
	public function adddepartment(Request $request)
	{
		if($request->isMethod('post'))
		{
			$user = new User();
			$user->name = $request['name'];
			// $lastuser = User::where('type','1')->orderBy('id','desc')->first();
			// $lastid = intval($lastuser->account)+1;
			// $user->account = sprintf("%05d",$lastid);
            $user->account = $request['name'];
			$user->role = $request['role'];
			$user->password = bcrypt('123456');
			$user->province = $request['province'];
			$user->city = $request['city'];
			$user->district = $request['district'];
			$user->street = $request['street'];
			$user['type'] = 1;
			$user->save();
			
			return redirect()->to(route('admin.user.userlist'))->with(['status'=>'添加成功']);
		}
		$provinces=Area::where('level',1)->select('id','name')->get();
        $citys=Area::where('level',2)->where('pid',370000)->select('id','name')->get();
        $districts=Area::where('level',3)->where('pid',371100)->select('id','name')->get();
        $streets=Area::where('level',4)->where('pid',371102)->select('id','name')->get();
        $role=Auth::guard('admin')->user()->role;
		return view('admin/department/adddepartment',compact('provinces','citys','districts','streets','role'));		
	}
	
	public function addstreetuser(Request $request)
	{
		if($request->isMethod('post'))
		{
            // $type = $request['type_id'];
            // if(intval($type)==1){
            //     $zold=User::where('role','street_admin')->where('street',$request['street'])->first();
            //     if($zold){
            //         return redirect(route('admin.user.userlist'))->withErrors(['status'=>'该街道已存在管理员，无法再创建']);
            //     }
            // }else{
            //     $zold=User::where('role','community_admin')->where('community',$request['community'])->first();
            //     if($zold){
            //         return redirect(route('admin.user.userlist'))->withErrors(['status'=>'该社区已存在管理员，无法再创建']);
            //     }
            // }
            $zold=User::where('role','community_admin')->where('community',$request['community'])->first();
            if($zold){
                return redirect(route('admin.user.userlist'))->withErrors(['status'=>'该社区已存在管理员，无法再创建']);
            }



			$user = new User();
			$user->name = $request['name'];
            $user->account = $request['name'];
            $user->province=$request['province']?$request['province']:null;
            $user->city=$request['city']?$request['city']:null;
            $user->district=$request['district']?$request['district']:null;
            $user->street=$request['street']?$request['street']:null;
            $user->community=$request['community']?$request['community']:null;
            $user->password=bcrypt('123456');
            // $user['type'] = 1;
            // if(intval($type)==1){
            //     $user->role = 'street_admin';
            // }else{
            //     $user->role = 'community_admin';
            // }
            $user->role = 'community_admin';
			$user->save();
			
			return redirect()->to(route('admin.user.userlist'))->with(['status'=>'添加成功']);
		}
        $type=2;
        $role=Auth::guard('admin')->user()->role;

        $provinces=Area::where('level',1)->select('id','name')->get();
        $citys=Area::where('level',2)->where('pid',370000)->select('id','name')->get();
        $districts=Area::where('level',3)->where('pid',370200)->select('id','name')->get();
        $streets=Area::where('level',4)->where('pid',370212)->select('id','name')->get();
        $communitys=Area::where('level',5)->where('pid',370212004)->select('id','name')->get();
        return view('admin/department/addstreetuser',compact('provinces','citys','districts','streets','communitys','role','type'));	
	}
}
